package com.bringway.global.domain;

import java.util.Iterator;

import org.apache.commons.lang.StringUtils;

import com.bringway.global.util.CodeUtils;

public class SimpleExcelDataModel extends ExcelDataModel {

	private String	 sheetName;

	private String	 outputFileName;

	private String[]   mainColumnTitles;

	private String[]   columnTitles;
	
	
	private String[][] complexTitles;

	private short[]	columnWidth;
	
	private String[]  extraInfo;

	private Object[][] data;
	
	private String[][] titleTags;
	
	private int[] scale;
	
	private Iterator iterator;

	//合计金额
	private Object[] sumInfo;
	
	public String[] getColumnTitles() {
		return columnTitles;
	}

	public void setColumnTitles(String[] columnTitles) {
		this.columnTitles = columnTitles;
		if (complexTitles==null){
			complexTitles = new String[1][];
			complexTitles[0] = columnTitles;
		}else{
			String[][] temp = new String[2][];
			temp[0] = complexTitles[0];
			temp[1] = columnTitles;
			complexTitles = temp;
		}
	}

	public Object[][] getData() {
		return data;
	}

	public void setData(Object[][] data) {
		this.data = data;
	}

	public short[] getColumnWidth() {
		return columnWidth;
	}

	public void setColumnWidth(short[] columnWidth) {
		this.columnWidth = columnWidth;
	}

	public String[] getMainColumnTitles() {
		return mainColumnTitles;
	}

	public void setMainColumnTitles(String[] mainColumnTiles) {
		this.mainColumnTitles = mainColumnTiles;
		if (complexTitles==null){
			complexTitles = new String[1][];
			complexTitles[0] = mainColumnTiles;
		}else{
			String[][] temp = new String[2][];
			temp[0] = mainColumnTiles;
			temp[1] = complexTitles[0];
			complexTitles = temp;
		}
	}

	public String getOutputFileName() {
		return outputFileName;
	}

	public void setOutputFileName(String outputFileName) {
		this.outputFileName = outputFileName;
	}

	public String getSheetName() {
		if (sheetName == null) {
			return getTitle();
		}
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public String[] getExtraInfo() {
		return extraInfo;
	}

	public void setExtraInfo(String[] extraInfo) {
		this.extraInfo = extraInfo;
	}

	public String[][] getComplexTitles() {
		return complexTitles;
	}

	public void setComplexTitles(String[][] complexTitles) {
		this.complexTitles = complexTitles;
	}
	
	
	public Iterator getIterator() {
		return iterator;
	}

	public void setIterator(Iterator iterator) {
		this.iterator = iterator;
	}

	public int[] getScale() {
		return scale;
	}

	public void setScale(int[] scale) {
		this.scale = scale;
	}

	public void  tag(){
		int colSize = getComplexTitles()[0].length;
		int rowSize = getComplexTitles().length;
		titleTags = new String[rowSize][colSize];
		for (int i = 0; i < rowSize; i++) {
			if (i==0){
				String maxCode = "000";
				for (int j = 0; j < colSize; j++) {
					if (StringUtils.isNotEmpty(getComplexTitles()[i][j])){
						 maxCode = CodeUtils.autoCode(maxCode,1,3);
					}
					titleTags[i][j] = maxCode ;
				}
			}else{
				String maxCode = "000";
				String lastParent = null;
				for (int j = 0; j < colSize; j++) {
					if (StringUtils.isNotEmpty(getComplexTitles()[i][j])) {
						if (StringUtils.equals(lastParent,
								titleTags[i - 1][j])) {
							maxCode = CodeUtils.autoCode(maxCode, 1, 3);
							lastParent = titleTags[i - 1][j];
						} else {
							maxCode = CodeUtils.autoCode(titleTags[i - 1][j]
									+ "000", 1, 3);
							lastParent = titleTags[i - 1][j];
						}
					}else{
						if (StringUtils.equals(lastParent,
								titleTags[i - 1][j])) {
							//titleTags[i][j] = maxCode ;
						} else {
							maxCode = CodeUtils.autoCode(titleTags[i - 1][j]
									+ "000", 1, 3);
							lastParent = titleTags[i - 1][j];
						}
					}
					titleTags[i][j] = maxCode ;
				}
			}
		}
		
	}
	
	public  boolean shouldMerge(int row,int i,int j){
		
		String p1 = titleTags[row][i];;
		String	p2 = titleTags[row][j];
		
		return StringUtils.equals(p1,p2);
	}
	
	public int getMergeAcross(int i,int j){
		int k = j+1;
		String t1 = titleTags[i][j];
		for (k=j+1; k < complexTitles[i].length; k++) {
			String t2 = titleTags[i][k];
			if (!StringUtils.equals(t1, t2)){
				break;
			}
		}
		return (k-j-1);
	}
	
	public int getMergeDown(int i,int j){
		int k = i+1;
		String t1 = titleTags[i][j];
		for (k=i+1; k < complexTitles.length; k++) {
			String t2 = titleTags[k][j];
			if (!StringUtils.equals(t1, t2)){
				break;
			}
		}
		return (k-i-1);
	}

	public void merge() {
		
		int colSize = getComplexTitles()[0].length;
		int rowSize = getComplexTitles().length;
		titleTags = new String[rowSize][colSize];
		for (int i = 0; i < rowSize; i++) {
			for (int j = 0; j < colSize; j++) {
				titleTags[i][j] = complexTitles[i][j];
			}
		}
		
		
		for (int i = 0; i < colSize ; i++) {
			for (int j = 0; j < rowSize; j++) {
				if (StringUtils.isNotEmpty(complexTitles[j][i])){
					for (int k = j+1; k < rowSize; k++) {
						if (StringUtils.isEmpty( complexTitles[k][i] )){
							titleTags[k][i] =  complexTitles[j][i];
						}else{
							break;
						}
					}
				}
			}
		}
		
		for (int i = 0; i < rowSize; i++) {
			for (int j = 0; j < colSize; j++) {
				if (StringUtils.isNotEmpty(titleTags[i][j])){
					for (int k = j+1; k < colSize; k++) {
						if (StringUtils.isEmpty( titleTags[i][k] )){
							titleTags[i][k] =  titleTags[i][j];
						}else{
							break;
						}
					}
				}
			}
		}
		
		
	}

	public Object[] getSumInfo() {
		return sumInfo;
	}

	public void setSumInfo(Object[] sumInfo) {
		this.sumInfo = sumInfo;
	}

}
